<?php 
/*
 * @灯商云端
 * @功能说明：单项目自定义函数集
 * @更新说明：暂无更新
 * @文件名 function.php 
 * @编码 UTF-8
 * @创建时间 2015-1-30 下午6:02:20
 * @创建人 XING
 */

/*
 * 检查用户登陆状态
 * @date: 2015-3-21上午10:55:57
 * @editor: XING
 */
if(!function_exists('is_login')){
	function is_login(){
		global $isLogin;
		$isLogin = false;
		$loginCookie = substr(cookie('ajwUid'),0,-1);
		if(isset($loginCookie) && $loginCookie){
			$loginUid = base64_decode($loginCookie);
			if(session($loginUid)){
				$isLogin = true;
			}else{
				$rememberNum = M('ResEntity')->getFieldByUsername($loginUid,'remember_token');
				if($rememberNum == cookie('_ajwunion')){
					$isLogin = true;
				}
			}
		}
		
		return $isLogin;
	}
}

/*
 * 获取登陆用户的登陆信息
 * @date: 2015-3-21上午11:00:13
 * @editor: XING
 */
if(!function_exists('getLoginMsg')){
	function getLoginMsg(){
		global $isLogin;
		$loginMsgObj = new stdClass();
		$loginMsgObj->isLogin = $isLogin;
		$loginMsgObj->username = base64_decode(substr(cookie('ajwUid'),0,-1));
		$users = M('ResEntity')->where(array('username'=>$loginMsgObj->username))->find();
		$loginMsgObj->rid = $users['rid'];
		$loginMsgObj->email = $users['email'];
		
		return $loginMsgObj;
	}
}

/*
 * 返回随机字符串
 * @date: 2015-3-21下午3:34:47
 * @editor: XING
 */
if(!function_exists('randstr')){
	function randstr($num=1){
		$return = '';
		$randstr = '1023456789abcdefghijklmnopqrstuvwxyz';
		for($i=0;$i<$num;$i++){
			$return .= $randstr{rand(0,35)};
		}
		
		return $return;
	}
}

/*
 * 对象转数组
 * @date: 2015-3-22下午1:33:12
 * @editor: XING
 */
function objectToArray($e){
	$e=(array)$e;
	foreach($e as $k=>$v){
		if( gettype($v)=='resource' ) return;
		if( gettype($v)=='object' || gettype($v)=='array' )
			$e[$k]=(array)objectToArray($v);
	}
	return $e;
}

/*
 * 日志添加记录
 * @date: 2015-4-1下午5:54:56
 * @editor: XING
 */
if(!function_exists('putlog')){
	function putlog($msg,$controller,$fun){
		M('SysLogs')->add(array(
			'sys_controller_name' => $controller,
			'sys_function_name' => $fun,
			'sys_remark' => (I('post.username') ? I('post.username') : '').$msg,
			'sys_editor' => getLoginMsg()->rid,
			'add_at' => time(),
			'update_at' => time()
		));
	}
}

/*
 * 获取分类的级别数
 * @date: 2015-4-7下午1:41:03
 * @editor: XING
 */
if(!function_exists('getCates')){
	function getCates($n){
		$fcate = M('CateEntity')->where(array(
					'parent_id' => '0'
				))->order(array('sort'=>'desc'))->select();
		if($fcate){
			foreach($fcate AS $key=>$val){
				$scate = M('CateEntity')->where(array(
						'parent_id' => $val['cate_id']	
					))->order(array(
							'sort' => 'desc'
					))->select();
				$outs[$key] = array(
						'key' => $val['cate_id'],
						'name' => $val['cate_name'],
						'part' => $val['parent_id'],
						'sons' => array()
					);
				if($n > 1){
					if($scate){
					/* 	foreach($scate AS $v){
							$outs[$key]['sons'][] = array(
									'key' => $v['cate_id'],
									'part' => $v['parent_id'],
									'name' => $v['cate_name'],
								);
						} */
					}
				}
			}
		}
		
		return $outs;
	}
}

/*
 * JSON格式化输出
 * @date: 2015-12-13 下午5:09:55
 * @editor: XING
 */
if(!function_exists('dataFormat')){
	function dataFormat($data=array(),$recode=1,$msg='',$adds=array()){
		if($data || $adds){
			echo json_encode(array_merge(array(
					'data' => $data,
					'code' => $recode,
					'msg' => $msg ? $msg : 'success'
			),$adds));exit;
		}else{
			echo json_encode(array_merge(array(
					'code' => $recode,
					'msg' => $msg ? $msg : '没有相关数据'
			)));exit;
		}
	}
}
?>